package com.see51.demo.see51demo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import com.ic70.kkplayer.sdk.CkkMediaInfo;
import com.ic70.kkplayer.sdk.CKKMoviePath;
import com.ic70.kkplayer.sdk.GLES2_0_SurfaceView;
import com.ic70.kkplayer.sdk.IKKPlayerErrNotify;
import com.ic70.kkplayer.sdk.CKKPlayerGlRender;
import com.see51.sdk.java.Jni51SeeSdk;

/**
 * Created by saint on 2016/4/27.
 * Video UI
 */
public class CPlayerActivity extends Activity implements IKKPlayerErrNotify
{
    //private GLSurfaceView glView;
    private  GLES2_0_SurfaceView glView;
    private  CKKPlayerGlRender   m_KKPlayer=null;
    private boolean m_bSeekPlayer=false;
    Activity m_PlayerActivity=null;
    boolean m_bNecState=true;
    int m_MinRealtimeDelay=3;
    int m_RaType=0;
    String CurTimeStr = new String();
    String TotalTimeStr = new String();
    Timer Activitytimer = new Timer();

    private String m_strDevId="";
    private String m_strTurnIp="";
    private Lock m_Avtivitylock = new ReentrantLock();
    int WhereInTime=0;
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            Activityhandler.sendMessage(message);
        }
    }; /* */
   public  enum  EnumPlayerStata
   {
       IniFirst,
       OpenFailure,
       Stop,
       Play,
       PAUSE
   }
    EnumPlayerStata PlayerStata=EnumPlayerStata.IniFirst;
    int m_CurTime=0;
    String MoviePathStr;
    int m_OpenCouner=0;
    int m_LastDisConnect=0;
    int m_nLastSerial=0;
    Handler Activityhandler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            m_Avtivitylock.lock();
            WhereInTime=1;
            m_Avtivitylock.unlock();
            if (msg.what == 1) {
                 if( m_KKPlayer!=null&&!m_bSeekPlayer)
                 {
                     CkkMediaInfo info = m_KKPlayer.GetCMediaInfo();
                 }
            }else if (msg.what == 2){
                    int err=msg.arg2;
                    if(err ==CKKPlayerGlRender.KKAVOver) {
                        Button NetButton = (Button) findViewById(R.id.NetButton);
                        NetButton.setText("视频播放结束");
                        NetButton.setVisibility(View.VISIBLE);
                    }else  if( err== CKKPlayerGlRender.KKOpenUrlOkFailure ) {
                        PlayerStata = EnumPlayerStata.OpenFailure;
                        Button NetButton = (Button) findViewById(R.id.NetButton);
                        NetButton.setText("文件打开失败");
                        NetButton.setVisibility(View.VISIBLE);
                        PlayerStata=EnumPlayerStata.OpenFailure;
                    }else if (err== CKKPlayerGlRender.KKAVWait) {
                        ///需要缓冲

                    } else if (err==CKKPlayerGlRender.KKAVReady||err !=CKKPlayerGlRender.KKOpenUrlOk) {
                        Button NetButton = (Button) findViewById(R.id.NetButton);
                        NetButton.setVisibility(View.GONE);
                    }else if (err==CKKPlayerGlRender.KKSeekOk || err==CKKPlayerGlRender.KKSeekErr) {

                    }
            }
            super.handleMessage(msg);
            m_Avtivitylock.lock();
            WhereInTime=0;
            m_Avtivitylock.unlock();
        };
    };


    ///播放器状态通知
    public void OpenMediaStateNotify(int obj,int err) {

        Log.v("libgl2jni","errcode:"+err);
        Message message = new Message();
        message.what = 2;
        message.arg1 =obj;
        message.arg2 = err;
        Activityhandler.sendMessage(message);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movielayout);
        m_PlayerActivity=this;
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Activitytimer.schedule(task, 500, 500);

        Log.v("libgl2jni","xxx");
        //glView = new GLES2_0_SurfaceView(this);// GLSurfaceView(this);
        glView =  (GLES2_0_SurfaceView) findViewById(R.id.GlsurfaceView);
        glView.setRenderer(m_KKPlayer); // Use a custom renderer
        ///不保持长宽比
        m_KKPlayer.SetKeepRatio( 0);
        glView.getAlpha();
        FrameLayout MovieFrameLayout = (FrameLayout) findViewById(R.id.MovieFrameLayout);
        //MovieFrameLayout.addView(glView, 0);

        Button NetButton= ( Button) findViewById(R.id.NetButton);

        NetButton.setOnClickListener(new Button.OnClickListener(){//创建监听
            public void onClick(View v) {
                /*Button NetButton2=(Button)v;
                NetButton2.setVisibility(View.GONE);
                m_KKPlayer.OpenMedia(MoviePathStr);*/
            }

        });
        Bundle bundle = getIntent().getExtras();
        CharSequence MoviePath = bundle.getCharSequence("MoviePath");
        MoviePathStr= MoviePath.toString(); /**/

        CharSequence  CRaType= bundle.getCharSequence("RaType");
        m_RaType= Integer.parseInt(CRaType.toString());
        CharSequence kkMediacodec= bundle.getCharSequence("kkMediacodec");
        int nMediacodec=Integer.parseInt(kkMediacodec.toString());


        CharSequence DevId     = bundle.getCharSequence("DevId");
        CharSequence DevTurnIp = bundle.getCharSequence("DevTurnIp");
        m_strDevId=DevId.toString();
        m_strTurnIp=DevTurnIp.toString();


        AdJustControl();
        m_KKPlayer.SetKeepRatio(0);
        m_KKPlayer.SetPlayerStateNotify(this);
        m_KKPlayer.SetDecoderMethod(0);

        m_CurTime=0;


        Button Btn_Mirror=(Button) findViewById(R.id.Btn_Mirror);
        Btn_Mirror.setOnClickListener(OnBtn_Mirror);

        Button Btn_OverTurn=(Button) findViewById(R.id.Btn_OverTurn);
        Btn_OverTurn.setOnClickListener(OnBtn_OverTurn);

        Button Btn_Up=(Button) findViewById(R.id.Btn_Up);
        Btn_Up.setOnClickListener(OnBtn_Up);

        Button Btn_Dom=(Button) findViewById(R.id.Btn_Dom);
        Btn_Dom.setOnClickListener(OnBtn_Down);

        Button Btn_Left=(Button) findViewById(R.id.Btn_Left);
        Btn_Left.setOnClickListener(OnBtn_Left);

        Button Btn_Right=(Button) findViewById(R.id.Btn_Right);
        Btn_Right.setOnClickListener(OnBtn_Right);

        RadioGroup RGroup_VideoMAX=(RadioGroup) findViewById(R.id.RGroup_VideoMAX);
        RGroup_VideoMAX.setOnCheckedChangeListener(OnRGroup_VideoMAX);
    }
    View.OnClickListener OnBtn_OverTurn = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,16);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };
    View.OnClickListener OnBtn_Mirror = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,15);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };

    View.OnClickListener OnBtn_Up = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,3);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };
    View.OnClickListener OnBtn_Down = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,4);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };

    View.OnClickListener OnBtn_Left = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,1);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };
    View.OnClickListener OnBtn_Right = new  View.OnClickListener()
    {

        public void onClick(View var1)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            if(devh!=0){
                sdk.JniSetDevAction(devh,2);
                sdk.JniReleaseDevHandle(devh);
            }
        };
    };
   RadioGroup.OnCheckedChangeListener OnRGroup_VideoMAX = new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup var1, int var2)
        {
            Jni51SeeSdk sdk = new Jni51SeeSdk();
            int devh= sdk.JniGetDevHandle(   m_strDevId, m_strTurnIp);
            switch (var2) {
                case R.id.rbtn_Hight:
                    //质量高
                    sdk .JniSee51SDKSetVideoQuality(devh,1);
                    break;
                case R.id.rbtn_Low:
                    //质量底
                    sdk .JniSee51SDKSetVideoQuality(devh,3);
                    break;
                case R.id. rbtn_Mid:
                    //中
                    sdk .JniSee51SDKSetVideoQuality(devh,2);
                    break;
            }
            if(devh!=0){
                sdk.JniReleaseDevHandle(devh);
            }

        }

    };
    public void WaitGif()
    {

    }
    public void onConfigurationChanged (Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        AdJustControl();
    }
    public void onStart() {
            super.onStart();
            if(PlayerStata==EnumPlayerStata.IniFirst) {
             Log.d("xxx","xx");
                m_KKPlayer.OpenMedia(MoviePathStr, true);
                m_KKPlayer.SetMinRealtimeDelay(m_MinRealtimeDelay);
                m_CurTime = 0;
                PlayerStata = EnumPlayerStata.Play;
            }else if(PlayerStata==EnumPlayerStata.PAUSE) {

            }
    }

    public void onStop()
    {
        super.onStop();
        if(PlayerStata==EnumPlayerStata.Play)
        {

        }
    }
    void AdJustControl()
    {
        Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
        int ori = mConfiguration.orientation ; //获取屏幕方向
        boolean Sp=false;
        if(ori == Configuration.ORIENTATION_LANDSCAPE) {
            //横屏
            Log.v("Screen", "横屏");
            Sp = true;
        }
    }
    public CPlayerActivity()
    {
        m_KKPlayer = new CKKPlayerGlRender();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            Log.v("KEYCODE_BACK", "KEYCODE_BACK");
            Activitytimer.cancel();
            PlayerStata=EnumPlayerStata.Stop;
            m_Avtivitylock.lock();
            while(WhereInTime==1)
            {
                ;
            }
            m_Avtivitylock.unlock();
            CKKPlayerGlRender KKPlayer= m_KKPlayer;
            m_KKPlayer=null;
            KKPlayer.KKDel();
            KKPlayer=null;
            Log.v("MoviePath", "Del");
            finish();
            Log.v("MoviePath", "退出 over mmm");
        }
        return false;
    }
}
